/*
 * widget.cpp
 *
 *  Created on: Dec 22, 2014
 *      Author: moje
 */


// this file has the drawing code for our custom widget

#include "widget.h"
#include <cairomm/context.h>

#include <iostream>

MetronomeWidget::MetronomeWidget()
{
  frequency = 440;

  // make the click events call the on_button_press_event function
  add_events( Gdk::BUTTON_PRESS_MASK );

  // make the click event do that function
  signal_button_press_event().connect( sigc::mem_fun(*this, &MetronomeWidget::on_button_press_event) );

  set_size_request(75,170);
}


void MetronomeWidget::update(int msg) {
	message = msg;
	redraw();
}

void MetronomeWidget::redraw() {
	// ask GTK to redraw when it suits
	Glib::RefPtr<Gdk::Window> win = get_window();
	if (win) {
		Gdk::Rectangle r(0, 0, get_allocation().get_width(),
				get_allocation().get_height());
		win->invalidate_rect(r, false);
	}
}

bool MetronomeWidget::on_button_press_event(GdkEventButton* event)
{
  // here we handle mouse clicks
  if ( event->button == 1 )
  {
    // normal click, so write Y co-ordinate to class variable frequceny
    float height = get_allocation().get_height();

    // see metronome.ttl for details, 880 - 40 is the port's value range
    frequency = 880 - (840 * (event->y/height))   + 40;

    std::cout << "Click, freq now = " << frequency << std::endl;

    // which port to write to, check your .ttl file for the index of
    // each port
    //int portNumber = 0;

    // here we use the LV2UI_Controller and LV2UI_write_function "things"
    // to write some data to a port
    //write_function( controller, portNumber, sizeof(float), 0, (const void*)&frequency);
  }


  redraw();
  return true;
}

bool MetronomeWidget::on_expose_event(GdkEventExpose* event)
{
  // This is where we draw on the window
  Glib::RefPtr<Gdk::Window> window = get_window();
  if(window)
  {
    Gtk::Allocation allocation = get_allocation();
    const int width = allocation.get_width();
    const int height = allocation.get_height();

    // coordinates for the center of the window
    int xc, yc;
    xc = width / 2;
    yc = height / 2;

    Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
    cr->set_line_width(10.0);

    // clip to the area indicated by the expose event so that we only redraw
    // the portion of the window that needs to be redrawn
    cr->rectangle(event->area.x, event->area.y,
            event->area.width, event->area.height);
    cr->clip();

    // make background black
    cr->rectangle(event->area.x, event->area.y,
            event->area.width, event->area.height);
    cr->set_source_rgb(0.1, 0.1, 0.1);
    cr->fill();

    // draw vertical grey line down the middle
    cr->set_source_rgb(0.3, 0.3, 0.3);
    cr->move_to(xc, 0);
    cr->line_to(xc, height);
    cr->stroke();

    // draw horizontal blue at frequency height
    float freqPix = height - (height*(frequency-40)/840.f); // convert to pix
    std::cout << "FreqPix " << freqPix << std::endl;
    printf("red = %d\n", message);
    cr->set_source_rgba(message, 0.1, 0.8,0.6);
    cr->move_to(20      , freqPix);
    cr->line_to(width-20, freqPix);
    cr->stroke();
  }

  return true;
}





